package webdev.danagtravel;

import java.util.Calendar;

import webdev.database.SQLQuery;
import webdev.dto.BootKingDTO;
import webdev.dto.DiaDiemDTO;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import dev.danang.R;

public class BookingActivity extends Activity implements OnClickListener {

	private Bundle bundle;
	
	private DatePicker dpArrive;
	private DatePicker dpLeave;
	
	private int yearArrive;
	private int monthArrive;
	private int dayArrive;
	
	private int yearLeave;
	private int monthLeave;
	private int dayLeave;
	
	private String timeArrive;
	private String timeLeave;
	
	private EditText mEdtComment;
	
	private ImageView mBtnBack;
	private TextView mTxtTitle;
	
	private Button mBtnDone;
	
	private DiaDiemDTO diaDiem;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_booking);
		init();
		setValue();
		setEvent();
		setCurrentDateOnView();
	}
	
	private void init() {
		dpArrive = (DatePicker) findViewById(R.id.ngay_den);
		dpLeave = (DatePicker) findViewById(R.id.ngay_di);
		mEdtComment = (EditText) findViewById(R.id.edt_comment);
		mBtnDone = (Button) findViewById(R.id.btn_post_booking);
		mBtnBack = (ImageView) findViewById(R.id.img_back);
		mTxtTitle = (TextView) findViewById(R.id.text_view_header_title);
	}
	
	private void setValue() {
		bundle = this.getIntent().getExtras();
		diaDiem = bundle.getParcelable("diadiem");
		mTxtTitle.setText(diaDiem.getTitle());
	}
	
	private void setEvent() {
		dpArrive.setOnClickListener(this);
		dpLeave.setOnClickListener(this);
		mBtnDone.setOnClickListener(this);
		mBtnBack.setOnClickListener(this);
	}
	
	// Display current date
	private void setCurrentDateOnView() {
		final Calendar c = Calendar.getInstance();
		yearArrive = c.get(Calendar.YEAR);
		monthArrive = c.get(Calendar.MONTH);
		dayArrive = c.get(Calendar.DAY_OF_MONTH);
		
		dpArrive.init(yearArrive, monthArrive, dayArrive, null);
		dpLeave.init(yearArrive, monthArrive, dayArrive, null);
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ngay_den:
			showDialog(0);
			break;
		case R.id.ngay_di:
			showDialog(1);
			break;
		case R.id.btn_post_booking:
			addData();
			break;
		case R.id.img_back:
			onBackPressed();
			break;
		default:
			break;
		}
	}
	
	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id) {
		switch (id) {
        case 0:
           // set date picker as current date
           return new DatePickerDialog(this, datePickerListener, yearArrive, monthArrive, dayArrive);
        }
        return null;

	}
	
	private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
		
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			
		}
	};
	
	private void addData() {
		BootKingDTO bk = new BootKingDTO();
		// id - name places
		bk.setNamePlace(String.valueOf(diaDiem.getId()));
		// set time 
		bk.setTimeArrive(timeArrive);
		bk.setTimeLeave(timeLeave);
		
		SQLQuery sql = new SQLQuery(getApplicationContext());
		sql.addBooking(bk);
	}
	
	
}
